<?php

namespace app\controller\admin\cnf;

use core\controller\AdminBase;
use app\bll\ComponentElement as BllComponentElement;
use Exception;
use support\View;
use Noodlehaus\Config;

/**
 * 表单元素控制器
 */
class ComponentElement extends AdminBase
{
    public function index()
    {

        $bllComponentElement = new BllComponentElement();
        $elements = $bllComponentElement->paginator(
            title: request()->get('title', ''),
            ident: request()->get('ident', ''),
        ); //已安装的元素控件

        View::assign('elements', $elements);
        return $this->view();
    }


    public function install()
    {
        if (is_post()) {
            try {
                $bllComponentElement = new BllComponentElement();
                $bllComponentElement->install();
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view(frame: false);
        }
    }


    public function handleUpgrade()
    {
        if (is_post()) {
            try {
                $bllComponentElement = new BllComponentElement();
                $bllComponentElement->handleUpgrade(request()->input('id'));
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view(frame: false);
        }
    }

    public function onlineUpgrade()
    {

        try {
            $bllComponentElement = new BllComponentElement();
            $bllComponentElement->onlineUpgrade(request()->input('id'));
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }


    public  function uninstall()
    {
        if (is_post()) {
            try {
                $bllComponentElement = new BllComponentElement();
                $bllComponentElement->uninstall(request()->input('id'));
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        }
    }


    public function setConfig()
    {
        $id                  = request()->get('id');
        $bllComponentElement = new BllComponentElement();
        $elementinfo         = $bllComponentElement->find($id);


        if (is_post()) {
            try {
                $config = Config::load(get_element_path() . $elementinfo['ident'] . '/config.ini');
                $data = request()->post();
                foreach ($data as $key => $d) {
                    $config->set('value.' . $key, $d);
                }
                $config->toFile(get_element_path() . $elementinfo['ident'] . '/config.ini');
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            $config = Config::load(get_element_path() . $elementinfo['ident'] . '/config.ini');
            View::assign('config', $config->all());

            return $this->view(
                frame: false
            );
        }
    }
}
